package com.sunday.pinxinghui.adapter;

import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.widgets.RecyclerTabLayout;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.LimitTime;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/8/17.
 */

public class LimitTimeTabAdapter extends RecyclerTabLayout.Adapter {



    private LimitTimePageAdapter pageAdapter;
    private int appWidth;

    public LimitTimeTabAdapter(ViewPager viewPager) {
        super(viewPager);
        pageAdapter = (LimitTimePageAdapter) viewPager.getAdapter();
        appWidth= DeviceUtils.getDisplay(pageAdapter.getContext()).widthPixels;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.time_tab_item, parent, false);
        RecyclerTabLayout.LayoutParams params=new RecyclerView.LayoutParams(appWidth/5
                , RecyclerView.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);

        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        LimitTime item=pageAdapter.getLimitTime(position);
        ListHolder listHolder= (ListHolder) holder;
        listHolder.timeTitle.setText(item.getStartTime()+":00");
        listHolder.timeExtra.setText(item.getType()==1?"已结束":item.getType()==2?"进行中":"预热中");
        if (position==getCurrentIndicatorPosition()){
            listHolder.timeTitle.setTextColor(Color.parseColor("#ffffff"));
            listHolder.timeExtra.setTextColor(Color.parseColor("#ffffff"));
            listHolder.layout.setBackground(ContextCompat.getDrawable(pageAdapter.getContext(),R.mipmap.qianggou_bg));
        }else {
            listHolder.timeTitle.setTextColor(Color.parseColor("#333333"));
            listHolder.timeExtra.setTextColor(Color.parseColor("#999999"));
            listHolder.layout.setBackground(ContextCompat.getDrawable(pageAdapter.getContext(),R.drawable.shape_white));
        }
    }

    @Override
    public int getItemCount() {
        return pageAdapter.getCount();
    }

    class ListHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.time_title)
        TextView timeTitle;
        @Bind(R.id.time_extra)
        TextView timeExtra;
        @Bind(R.id.layout)
        LinearLayout layout;

        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getViewPager().setCurrentItem(getAdapterPosition());
                }
            });
        }


    }
}
